import { CommodityListType, CreateOrderParamType, SHCancelParmaType, SHChangeNumType, SHListQueryType, ShoppingCar_AddType } from "@Tea/model"
import { AppTokenObj } from "../dao/AppToken"
import { ReposBase } from "./RepoBase"
import { delLastStr, renderInsertParam, renderInsertSql, renderSelectParam, renderSelectSql, renderUpdateSql } from "../utils/renderSql"

type ShoppingCarListType = Omit<CommodityListType, 'number'> & {orderNumber: number, shoppingId: number}
export class ShoppingCarRepository extends ReposBase {
  private static inst: ShoppingCarRepository

  public static getInstance() {
    if (!ShoppingCarRepository.inst) {
      ShoppingCarRepository.inst = new ShoppingCarRepository()
    }

    return ShoppingCarRepository.inst
  }

  addCommodity(teaId: ShoppingCar_AddType["teaId"], customer: AppTokenObj['userId']) { 
    const config: renderInsertParam = {
      database: "shopping_cars",
      keyAndVal: [
        { key: "tea_id", value: teaId },
        { key: "user_id", value: customer },
        { key: "order_number", value: 1 }
      ]
    }

    return this.insert(renderInsertSql(config))
  }

  shoppingCarList(query: SHListQueryType, customer: AppTokenObj['userId']) {
    const config: renderSelectParam = {
      database: [
        { name: 'shopping_cars', aliasName: "s" },
        { name: "commodities", aliasName: 'c' },
        { name: "tea_types", aliasName: 't' }
      ],
      returnFeild: [
        { name: "s.tea_id", aliasName: "teaId" },
        { name: 'c.tea_name', aliasName: 'teaName' },
        "cover",
        { name: 't.type_name', aliasName: 'typeName' },
        { name: 'c.tea_type', aliasName: 'teaType' },
        { name: 's.order_number', aliasName: 'orderNumber' },
        "price",
        { name: 's.shopping_id', aliasName: 'shoppingId' },
      ],
      where: [
        { key: 'c.tea_type', sqlVal: 't.type_id', isDatabase: true },
        { key: 's.tea_id', sqlVal: 'c.tea_id', isDatabase: true },
        { key: 's.user_id', sqlVal: customer },
      ],
      like:[]
    }

    if (query.teaId) {
      config.where.push(
        { key: 's.tea_id', sqlVal: query.teaId },
      )
    }

    if (query.teaName) {
      config.like.push(
        {key:'c.tea_name', sqlVal: query.teaName}
      )
    }

    const sql = renderSelectSql(config)

    return this.query<ShoppingCarListType>(sql)
  }

  // 通过shoppingId查询
  shoppingCarListByIds(query: CreateOrderParamType["shoppingIds"], customer: AppTokenObj['userId']) {
    const config: renderSelectParam = {
      database: [
        { name: 'shopping_cars', aliasName: "s" },
      ],
      returnFeild: [
        { name: "s.tea_id", aliasName: "teaId" },
        { name: 's.order_number', aliasName: 'orderNumber' },
        { name: 's.shopping_id', aliasName: 'shoppingId' },
      ],
      where: [
        { key: 's.user_id', sqlVal: customer },
      ],
    }

    const sql = renderSelectSql(config)

    let otherSql = ""
    query.forEach(id => {
      otherSql += `shopping_id = ${id} OR `
    })

    return this.query<ShoppingCarListType>(`${sql} AND (${delLastStr(otherSql, 3)})`)
  }

  changeNumber(params: SHChangeNumType, customer: AppTokenObj['userId']) { 
    const sql = renderUpdateSql({
      database: 'shopping_cars',
      keyAndVal: [
        {key:'order_number', value: params.orderNumber}
      ],
      where: [
        {key: 'shopping_id', sqlVal: params.shoppingId}
      ]
    })

    return this.update(sql)
  }

  cancelCommodity(params: SHCancelParmaType, customer: AppTokenObj['userId']) { 
    let orSql = ' '
    params.shoppingIds.forEach(id => {
      orSql += `shopping_id = ${id} OR `
    })

    return this.delete(`DELETE FROM shopping_cars Where user_id = ${customer} AND (${delLastStr(orSql, 3)})`)
  }
}